package com.etone.smartAudit.enums;

import com.etone.smartAudit.error.CustomParameterizedException;
import lombok.Getter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author: 蒋学伟
 * @create: 2020-06-17 17:15:13
 * @Description CheckState
 */
public enum CheckState {
    PASS("通过"),
    CHECKING("审核中"),
    EXECUTE_NOT_START("执行未开始"),
    EXECUTE_STARTING("执行中"),
    EXECUTE_CHECKING("执行审核中"),
    EXECUTE_REJECT("执行审核驳回"),
    REJECT("驳回"),
    ;

    @Getter
    private String value;

    CheckState(String value) {
        this.value = value;
    }

    /**
     * 校验下一步审核状态
     *
     * @param checkState 待验证的状态
     * @param state      上一步状态
     * @param type       任务类型
     * @return
     */
    public static Boolean checkNextState(CheckState checkState, CheckState state, TaskType type) {
        if (checkState == null || state == null || type == null) {
            return false;
        }
        List<CheckState> checkStates = allowNextState(state, type);
        return checkStates.contains(checkState);
    }

    /**
     * 获取下一步审核状态
     *
     * @param state
     * @param type
     * @return
     */
    public static List<CheckState> allowNextState(CheckState state, TaskType type) {
        List<CheckState> result = new ArrayList<>();
        if (TaskType.isItemTask(type) || type.equals(TaskType.DATA_MODEL_AUTH) || type.equals(TaskType.RESOURCE_DEVICE)) {
            //项目任务
            switch (state) {
                case CHECKING:
                    result.add(PASS);
                    result.add(REJECT);
                    result.add(CHECKING);
                    break;
                default:
                    throw new CustomParameterizedException("无效审核");
            }
        } else {
            //模型任务/工作任务/临时资源申请
            switch (state) {
                case CHECKING:
                    result.add(EXECUTE_NOT_START);
                    result.add(REJECT);
                    result.add(CHECKING);
                    break;
                case EXECUTE_NOT_START:
                    result.add(EXECUTE_STARTING);
                    result.add(EXECUTE_REJECT);
                    break;
                case EXECUTE_STARTING:

                    if (TaskType.getExecute_starting2PassTask(type)) {
                        result.add(PASS);
                    } else {
                        result.add(EXECUTE_CHECKING);
                        result.add(EXECUTE_REJECT);
                        result.add(EXECUTE_STARTING);
                    }
                    break;
                case EXECUTE_CHECKING:
                    result.add(PASS);
                    result.add(EXECUTE_REJECT);
                    break;
                case EXECUTE_REJECT:
                    result.add(EXECUTE_STARTING);
                    break;
                default:
                    throw new CustomParameterizedException("无效审核");
            }
        }
        return result;
    }

    /**
     * 状态是否可以删除
     *
     * @return
     */
    public static Boolean isArrowDelete(CheckState state) {
        List arrowDeleteStates = Arrays.asList(PASS, CHECKING, REJECT);
        return state != null && arrowDeleteStates.contains(state);
    }

    /**
     * 状态是否可以编辑
     *
     * @return
     */
    public static Boolean isArrowEdit(CheckState state) {
        List arrowEditStates = Arrays.asList(CHECKING, REJECT);
        return state != null && arrowEditStates.contains(state);
    }
}
